﻿using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

namespace XUtliPoolLib
{
	// Token: 0x0200004E RID: 78
	public class AssetBundleManager : MonoBehaviour
	{
		// Token: 0x17000062 RID: 98
		// (get) Token: 0x0600026F RID: 623 RVA: 0x00013874 File Offset: 0x00011A74
		public int BundleCount
		{
			get
			{
				return this._bundleCount;
			}
		}

		// Token: 0x06000270 RID: 624 RVA: 0x0001388C File Offset: 0x00011A8C
		public AssetBundleManager()
		{
			AssetBundleManager.Instance = this;
			this.pathResolver = new AssetBundlePathResolver();
			this._defaultHash = XSingleton<XCommon>.singleton.XHashLowerRelpaceDot(0U, "Assets.Resources.");
		}

		// Token: 0x17000063 RID: 99
		// (get) Token: 0x06000271 RID: 625 RVA: 0x00013938 File Offset: 0x00011B38
		public AssetBundleDataReader depInfoReader
		{
			get
			{
				return this._depInfoReader;
			}
		}

		// Token: 0x06000272 RID: 626 RVA: 0x00013950 File Offset: 0x00011B50
		protected void Awake()
		{
			base.InvokeRepeating("CheckUnusedBundle", 0f, 5f);
		}

		// Token: 0x06000273 RID: 627 RVA: 0x00013969 File Offset: 0x00011B69
		public void Init()
		{
			this.RemoveAll();
			this.LoadDepInfo();
		}

		// Token: 0x06000274 RID: 628 RVA: 0x0001397C File Offset: 0x00011B7C
		public void Init(byte[] data, Action callback)
		{
			if (data.Length > 4)
			{
				XBinaryReader xbinaryReader = XBinaryReader.Get();
				xbinaryReader.InitByte(data, 0, 0);
				if (xbinaryReader.ReadChar() == 'A' && xbinaryReader.ReadChar() == 'B' && xbinaryReader.ReadChar() == 'D')
				{
					if (xbinaryReader.ReadChar() == 'T')
					{
						this._depInfoReader = new AssetBundleDataReader();
					}
					else
					{
						this._depInfoReader = new AssetBundleDataBinaryReader();
					}
					this._depInfoReader.Read(xbinaryReader);
				}
				XBinaryReader.Return(xbinaryReader, false);
			}
			//bool flag4 = callback != null;
			if (callback != null)
			{
				callback();
			}
		}

		// Token: 0x06000275 RID: 629 RVA: 0x00013A1C File Offset: 0x00011C1C
		private void LoadDepInfo()
		{
			string path = string.Format("{0}/{1}", this.pathResolver.BundleCacheDir, this.pathResolver.DependFileName);
			if (File.Exists(path))
			{
				this.Init(File.ReadAllBytes(path), null);
			}
			else
			{
				TextAsset textAsset = Resources.Load<TextAsset>("dep");
				if (textAsset != null)
				{
					this.Init(textAsset.bytes, null);
					Resources.UnloadAsset(textAsset);
				}
				else
				{
					XSingleton<XDebug>.singleton.AddErrorLog("depFile not exist!", null, null, null, null, null);
				}
			}
		}

		// Token: 0x06000276 RID: 630 RVA: 0x00013AAF File Offset: 0x00011CAF
		private void OnDestroy()
		{
			this.RemoveAll();
		}

		// Token: 0x06000277 RID: 631 RVA: 0x00013ABC File Offset: 0x00011CBC
		public AssetBundleLoader Load(uint hash, string path, string suffix, string prefix = null, AssetBundleManager.LoadAssetCompleteHandler handler = null, int handlerID = -1)
		{
			AssetBundleLoader result;
			if (this.depInfoReader == null)
			{
				result = null;
			}
			else
			{
				AssetBundleLoader assetBundleLoader = this.CreateLoader(hash, path, suffix, prefix);
				if (assetBundleLoader == null)
				{
					result = null;
				}
				else
				{
					assetBundleLoader.loadHandlerID = handlerID;
					this._thisTimeLoaderSet.Add(assetBundleLoader);
					if (assetBundleLoader.isComplete)
					{
						if (handler != null)
						{
							handler(assetBundleLoader.bundleInfo, handlerID);
						}
					}
					else
					{
						if (handler != null)
						{
							AssetBundleLoader assetBundleLoader2 = assetBundleLoader;
							assetBundleLoader2.onComplete = (AssetBundleManager.LoadAssetCompleteHandler)Delegate.Combine(assetBundleLoader2.onComplete, handler);
						}
						this._isCurrentLoading = true;
						if (assetBundleLoader.state < LoadState.State_LoadingAsync)
						{
							this._nonCompleteLoaderSet.Add(assetBundleLoader);
						}
						this.StartLoad();
					}
					result = assetBundleLoader;
				}
			}
			return result;
		}

		// Token: 0x06000278 RID: 632 RVA: 0x00013B90 File Offset: 0x00011D90
		public AssetBundleInfo LoadImm(uint hash, string path, string suffix, string prefix = null)
		{
			//bool flag = this.depInfoReader == null;
			AssetBundleInfo result = null;
			if (this.depInfoReader != null)
			{
                AssetBundleLoader assetBundleLoader = this.CreateLoader(hash, path, suffix, prefix);
                //bool flag2 = assetBundleLoader == null;
                if (assetBundleLoader != null)
                {
					this._thisTimeLoaderSet.Add(assetBundleLoader);
                    //bool isComplete = assetBundleLoader.isComplete;
                    if (assetBundleLoader.isComplete)
                    {
                        result = assetBundleLoader.bundleInfo;
                    }
                    else
                    {
                        this._isCurrentLoading = true;
                        //bool flag3 = assetBundleLoader.state < LoadState.State_Loading;
                        if (assetBundleLoader.state < LoadState.State_Loading)
                        {
                            this._nonCompleteLoaderSet.Add(assetBundleLoader);
                        }
                        this.StartLoadImm();
                        result = assetBundleLoader.bundleInfo;
                    }
                }
            }
			return result;
		}

		// Token: 0x06000279 RID: 633 RVA: 0x00013C24 File Offset: 0x00011E24
		public bool CheckInDep(uint hash)
		{
			return this.depInfoReader != null && this._depInfoReader.GetAssetBundleInfo(hash) != null;
		}

		// Token: 0x0600027A RID: 634 RVA: 0x00013C5C File Offset: 0x00011E5C
		internal AssetBundleLoader CreateLoader(uint abFileName, string location = null, string suffix = null, string prefix = null)
		{
			bool bfindInCache = this._loaderCache.ContainsKey(abFileName);
			AssetBundleLoader assetBundleLoader = null;
			if (bfindInCache)
			{
				assetBundleLoader = this._loaderCache[abFileName];
			}
			else
			{
				AssetBundleData assetBundleData = this._depInfoReader.GetAssetBundleInfo(abFileName);
				//bool flag2 = assetBundleData == null;
				if (assetBundleData == null)
				{
					//bool flag3 = prefix != null;
					uint num;
					if (prefix != null)
					{
						num = XSingleton<XCommon>.singleton.XHashLowerRelpaceDot(0U, prefix);
					}
					else
					{
						num = this._defaultHash;
					}
					//bool flag4 = location != null;
					if (location != null)
					{
						num = XSingleton<XCommon>.singleton.XHashLowerRelpaceDot(num, location);
					}
					//bool flag5 = suffix != null;
					if (suffix != null)
					{
						num = XSingleton<XCommon>.singleton.XHashLowerRelpaceDot(num, suffix);
					}
					assetBundleData = this._depInfoReader.GetAssetBundleInfoByShortName(num);
				}
				//bool flag6 = assetBundleData == null;
				if (assetBundleData == null)
				{
					return null;
				}
				assetBundleLoader = this.CreateLoader();
				assetBundleLoader.bundleManager = this;
				assetBundleLoader.bundleData = assetBundleData;
				assetBundleLoader.bundleName = assetBundleData.fullName;
				this._loaderCache[abFileName] = assetBundleLoader;
			}
			return assetBundleLoader;
		}

		// Token: 0x0600027B RID: 635 RVA: 0x00013D60 File Offset: 0x00011F60
		protected virtual AssetBundleLoader CreateLoader()
		{
			RuntimePlatform platform = Application.platform;
			AssetBundleLoader result;
			if (platform != RuntimePlatform.IPhonePlayer)
			{
				if (platform != RuntimePlatform.Android)
				{
					result = new MobileAssetBundleLoader();
				}
				else
				{
					result = new AndroidAssetBundleLoader();
				}
			}
			else
			{
				result = new IOSAssetBundleLoader();
			}
			return result;
		}

		// Token: 0x0600027C RID: 636 RVA: 0x00013D9C File Offset: 0x00011F9C
		private void StartLoad()
		{
			if (this._nonCompleteLoaderSet.Count > 0)
			{
				List<AssetBundleLoader> list = ListPool<AssetBundleLoader>.Get();
				list.AddRange(this._nonCompleteLoaderSet);
				this._nonCompleteLoaderSet.Clear();
				foreach (AssetBundleLoader item in list)
				{
					this._currentLoadQueue.Add(item);
				}
				this._progress = new AssetBundleLoadProgress();
				this._progress.total = this._currentLoadQueue.Count;
				foreach (AssetBundleLoader assetBundleLoader in list)
				{
					assetBundleLoader.Load();
				}
				ListPool<AssetBundleLoader>.Release(list);
			}
		}

		// Token: 0x0600027D RID: 637 RVA: 0x00013E58 File Offset: 0x00012058
		private void StartLoadImm()
		{
			if (this._nonCompleteLoaderSet.Count > 0)
			{
				if (this._nonCompleteLoaderSet.Count == 1)
				{
					HashSet<AssetBundleLoader>.Enumerator enumerator = this._nonCompleteLoaderSet.GetEnumerator();
					enumerator.MoveNext();
					this._currentLoadQueue.Add(enumerator.Current);
					this._nonCompleteLoaderSet.Clear();
					this._progress.percent = 0f;
					this._progress.complete = 0;
					this._progress.loader = null;
					this._progress.total = this._currentLoadQueue.Count;
					enumerator.Current.LoadImm();
				}
				else
				{
					List<AssetBundleLoader> list = ListPool<AssetBundleLoader>.Get();
					list.AddRange(this._nonCompleteLoaderSet);
					this._nonCompleteLoaderSet.Clear();
					foreach (AssetBundleLoader item in list)
					{
						this._currentLoadQueue.Add(item);
					}
					this._progress.percent = 0f;
					this._progress.complete = 0;
					this._progress.loader = null;
					this._progress.total = this._currentLoadQueue.Count;
					foreach (AssetBundleLoader assetBundleLoader in list)
					{
						assetBundleLoader.LoadImm();
					}
					ListPool<AssetBundleLoader>.Release(list);
				}
			}
		}

		// Token: 0x0600027E RID: 638 RVA: 0x00013FD0 File Offset: 0x000121D0
		public void RemoveAll()
		{
			this._currentLoadQueue.Clear();
			this._requestQueue.Clear();
			foreach (KeyValuePair<uint, AssetBundleInfo> keyValuePair in this._loadedAssetBundle)
			{
				keyValuePair.Value.Dispose();
			}
			this._loadedAssetBundle.Clear();
			this._loaderCache.Clear();
			this._requestUnloadBundleQueue.Clear();
		}

		// Token: 0x0600027F RID: 639 RVA: 0x0001404C File Offset: 0x0001224C
		public AssetBundleInfo GetBundleInfo(uint key)
		{
			foreach (KeyValuePair<uint, AssetBundleInfo> keyValuePair in this._loadedAssetBundle)
			{
				AssetBundleInfo value = keyValuePair.Value;
				if (value.bundleName == key)
				{
					return value;
				}
			}
			return null;
		}

		// Token: 0x06000280 RID: 640 RVA: 0x000140A0 File Offset: 0x000122A0
		internal void RequestLoadBundle(AssetBundleLoader loader)
		{
			if (this._requestRemain < 0)
			{
				this._requestRemain = 0;
			}
			if (this._requestRemain == 0)
			{
				this._requestQueue.Enqueue(loader);
			}
			else
			{
				this.LoadBundle(loader);
			}
		}

		// Token: 0x06000281 RID: 641 RVA: 0x000140EC File Offset: 0x000122EC
		internal void RequestLoadBundleImm(AssetBundleLoader loader)
		{
			if (this._requestRemain < 0)
			{
				this._requestRemain = 0;
			}
			if (this._requestRemain == 0)
			{
				this._requestQueue.Enqueue(loader);
			}
			else
			{
				this.LoadBundleImm(loader);
			}
		}

		// Token: 0x06000282 RID: 642 RVA: 0x00014138 File Offset: 0x00012338
		private void CheckRequestList()
		{
			while (this._requestRemain > 0 && this._requestQueue.Count > 0)
			{
				AssetBundleLoader loader = this._requestQueue.Dequeue();
				this.LoadBundle(loader);
			}
		}

		// Token: 0x06000283 RID: 643 RVA: 0x0001417C File Offset: 0x0001237C
		private void LoadBundle(AssetBundleLoader loader)
		{
			if (!loader.isComplete)
			{
				loader.LoadBundle();
				this._requestRemain--;
			}
		}

		// Token: 0x06000284 RID: 644 RVA: 0x000141B0 File Offset: 0x000123B0
		private void LoadBundleImm(AssetBundleLoader loader)
		{
			if (!loader.isComplete)
			{
				loader.LoadBundleImm();
				this._requestRemain--;
			}
		}

		// Token: 0x06000285 RID: 645 RVA: 0x000141E2 File Offset: 0x000123E2
		internal void LoadError(AssetBundleLoader loader)
		{
			this.LoadComplete(loader);
		}

		// Token: 0x06000286 RID: 646 RVA: 0x000141F0 File Offset: 0x000123F0
		internal void LoadComplete(AssetBundleLoader loader)
		{
			this._requestRemain++;
			this._currentLoadQueue.Remove(loader);
			if (this.onProgress != null)
			{
				this._progress.loader = loader;
				this._progress.complete = this._progress.total - this._currentLoadQueue.Count;
				this.onProgress(this._progress);
			}
			if (this._currentLoadQueue.Count == 0 && this._nonCompleteLoaderSet.Count == 0)
			{
				this._isCurrentLoading = false;
				foreach (AssetBundleLoader assetBundleLoader in this._thisTimeLoaderSet)
				{
					if (assetBundleLoader.bundleInfo != null)
					{
						assetBundleLoader.bundleInfo.ResetLifeTime();
					}
				}
				this._thisTimeLoaderSet.Clear();
			}
			else
			{
				this.CheckRequestList();
			}
		}

		// Token: 0x06000287 RID: 647 RVA: 0x000142EC File Offset: 0x000124EC
		internal AssetBundleInfo CreateBundleInfo(AssetBundleLoader loader, AssetBundleInfo abi = null, AssetBundle assetBundle = null)
		{
			if (abi == null)
			{
				abi = new AssetBundleInfo();
			}
			abi.bundleName = loader.bundleName;
			abi.bundle = assetBundle;
			abi.data = loader.bundleData;
			this._loadedAssetBundle[abi.bundleName] = abi;
			this._bundleCount++;
			return abi;
		}

		// Token: 0x06000288 RID: 648 RVA: 0x0001434E File Offset: 0x0001254E
		public void DeleteBundleCount()
		{
			this._bundleCount--;
		}

		// Token: 0x06000289 RID: 649 RVA: 0x0001435F File Offset: 0x0001255F
		internal void RemoveBundleInfo(AssetBundleInfo abi)
		{
			abi.Dispose();
			this._loadedAssetBundle.Remove(abi.bundleName);
		}

		// Token: 0x17000064 RID: 100
		// (get) Token: 0x0600028A RID: 650 RVA: 0x0001437C File Offset: 0x0001257C
		public bool isCurrentLoading
		{
			get
			{
				return this._isCurrentLoading;
			}
		}

		// Token: 0x0600028B RID: 651 RVA: 0x00014394 File Offset: 0x00012594
		private void CheckUnusedBundle()
		{
			this.UnloadUnusedBundle(false);
		}

		// Token: 0x0600028C RID: 652 RVA: 0x000143A0 File Offset: 0x000125A0
		public void UnloadUnusedBundle(bool force = false)
		{
			bool flag = (!this._isCurrentLoading && !XSingleton<XResourceLoaderMgr>.singleton.isCurrentLoading) || force;
			if (flag)
			{
				List<uint> list = ListPool<uint>.Get();
				list.AddRange(this._loadedAssetBundle.Keys);
				int num = force ? 100000 : 20;
				int num2 = 0;
				bool flag3;
				do
				{
					flag3 = false;
					int num3 = 0;
					while (num3 < list.Count && flag && num2 < num)
					{
						uint key = list[num3];
						AssetBundleInfo assetBundleInfo = this._loadedAssetBundle[key];
						bool isUnused = assetBundleInfo.isUnused;
						if (isUnused)
						{
							flag3 = true;
							num2++;
							this.RemoveBundleInfo(assetBundleInfo);
							list.RemoveAt(num3);
							num3--;
						}
						num3++;
					}
				}
				while (flag3 && flag && num2 < num);
				ListPool<uint>.Release(list);
				while (this._requestUnloadBundleQueue.Count > 0 && flag && num2 < num)
				{
					AssetBundleInfo assetBundleInfo2 = this._requestUnloadBundleQueue.Dequeue();
					if (assetBundleInfo2 != null)
					{
						assetBundleInfo2.UnloadBundle();
						num2++;
					}
				}
			}
		}

		// Token: 0x0600028D RID: 653 RVA: 0x000144E0 File Offset: 0x000126E0
		public void UnloadNotUsedLoader()
		{
			List<uint> list = ListPool<uint>.Get();
			list.AddRange(this._loadedAssetBundle.Keys);
			List<AssetBundleLoader> list2 = ListPool<AssetBundleLoader>.Get();
			list2.AddRange(this._nonCompleteLoaderSet);
			list2.AddRange(this._currentLoadQueue);
			foreach (AssetBundleLoader assetBundleLoader in list2)
			{
				this.RemoveLoadingLoader(assetBundleLoader.bundleName, list);
			}
			for (int i = 0; i < list.Count; i++)
			{
				AssetBundleInfo abi = this._loadedAssetBundle[list[i]];
				this.RemoveBundleInfo(abi);
				list.RemoveAt(i);
				i--;
			}
			ListPool<uint>.Release(list);
			ListPool<AssetBundleLoader>.Release(list2);
		}

		// Token: 0x0600028E RID: 654 RVA: 0x000145A8 File Offset: 0x000127A8
		private void RemoveLoadingLoader(uint hash, List<uint> list)
		{
			AssetBundleData assetBundleInfo = this._depInfoReader.GetAssetBundleInfo(hash);
			list.Remove(assetBundleInfo.fullName);
			if (assetBundleInfo.dependencies != null)
			{
				for (int i = 0; i < assetBundleInfo.dependencies.Length; i++)
				{
					this.RemoveLoadingLoader(assetBundleInfo.dependencies[i], list);
				}
			}
		}

		// Token: 0x0600028F RID: 655 RVA: 0x00014608 File Offset: 0x00012808
		public void AddUnloadBundleQueue(AssetBundleInfo info)
		{
			this._requestUnloadBundleQueue.Enqueue(info);
			if (this._requestUnloadBundleQueue.Count > 3)
			{
				this.UnloadUnusedBundle(false);
			}
		}

		// Token: 0x06000290 RID: 656 RVA: 0x00014640 File Offset: 0x00012840
		public void RemoveBundle(uint key)
		{
			AssetBundleInfo bundleInfo = this.GetBundleInfo(key);
			if (bundleInfo != null)
			{
				this.RemoveBundleInfo(bundleInfo);
			}
		}

		// Token: 0x0400023D RID: 573
		public static Version version = new Version(0, 1, 0);

		// Token: 0x0400023E RID: 574
		public static AssetBundleManager Instance;

		// Token: 0x0400023F RID: 575
		public static string NAME = "AssetBundleManager";

		// Token: 0x04000240 RID: 576
		public static bool enableLog = false;

		// Token: 0x04000241 RID: 577
		private const int MAX_REQUEST = 100;

		// Token: 0x04000242 RID: 578
		private int _requestRemain = 100;

		// Token: 0x04000243 RID: 579
		private Queue<AssetBundleLoader> _requestQueue = new Queue<AssetBundleLoader>();

		// Token: 0x04000244 RID: 580
		private List<AssetBundleLoader> _currentLoadQueue = new List<AssetBundleLoader>();

		// Token: 0x04000245 RID: 581
		private HashSet<AssetBundleLoader> _nonCompleteLoaderSet = new HashSet<AssetBundleLoader>();

		// Token: 0x04000246 RID: 582
		private HashSet<AssetBundleLoader> _thisTimeLoaderSet = new HashSet<AssetBundleLoader>();

		// Token: 0x04000247 RID: 583
		private Dictionary<uint, AssetBundleInfo> _loadedAssetBundle = new Dictionary<uint, AssetBundleInfo>();

		// Token: 0x04000248 RID: 584
		private Dictionary<uint, AssetBundleLoader> _loaderCache = new Dictionary<uint, AssetBundleLoader>();

		// Token: 0x04000249 RID: 585
		private bool _isCurrentLoading;

		// Token: 0x0400024A RID: 586
		private Queue<AssetBundleInfo> _requestUnloadBundleQueue = new Queue<AssetBundleInfo>();

		// Token: 0x0400024B RID: 587
		private AssetBundleLoadProgress _progress = new AssetBundleLoadProgress();

		// Token: 0x0400024C RID: 588
		public AssetBundleManager.LoadProgressHandler onProgress;

		// Token: 0x0400024D RID: 589
		public AssetBundlePathResolver pathResolver;

		// Token: 0x0400024E RID: 590
		private AssetBundleDataReader _depInfoReader;

		// Token: 0x0400024F RID: 591
		private int _bundleCount = 0;

		// Token: 0x04000250 RID: 592
		private uint _defaultHash = 0U;

		// Token: 0x0200029D RID: 669
		// (Invoke) Token: 0x06000E02 RID: 3586
		public delegate void LoadAssetCompleteHandler(AssetBundleInfo info, int handlerID);

		// Token: 0x0200029E RID: 670
		// (Invoke) Token: 0x06000E06 RID: 3590
		public delegate void LoaderCompleteHandler(AssetBundleLoader info);

		// Token: 0x0200029F RID: 671
		// (Invoke) Token: 0x06000E0A RID: 3594
		public delegate void LoadProgressHandler(AssetBundleLoadProgress progress);
	}
}
